/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hello;

import com.sun.lwuit.Button;
import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.io.IOException;
import javax.microedition.midlet.MIDlet;

/**
 * @author Propietario
 */
public class HelloMIDlet extends MIDlet implements ActionListener {

    private void displayInit() throws IOException {
        Display.init(this);
        setDisplayTheme("/res/javaTheme.res", "javaTheme");
    }

    private void setDisplayTheme(String resourcePath, String themeName) throws IOException {
        Resources r = Resources.open(resourcePath);
        UIManager.getInstance().setThemeProps(r.getTheme(themeName));
    }

    private Form getImagesForm() throws IOException {
        Form f = new Form("Hello, LWUIT!");
        addFormCommands(f);
        Label label = new Label("Baldy");
        f.addComponent(label);

        Image image;

        //image = Image.createImage("/res/baldy.png");
        image = Image.createImage("/res/P1020776r.JPG");

        Label pictureLabel = new Label(image);
        f.addComponent(pictureLabel);

        Label bottomText = new Label(image);
        bottomText.setText("Baldassare");
        bottomText.setTextPosition(Component.BOTTOM);
        f.addComponent(bottomText);
        return f;
    }

    private Form getImagesButtonsForm() throws IOException {
        Form f = new Form("Hello, LWUIT!");
        addFormCommands(f);

        Image image = Image.createImage("/res/P1020776r.JPG");

        Button pictureButton = new Button(image);
        f.addComponent(pictureButton);

        Button combined = new Button("Baldassare", image);
        combined.setTextPosition(Component.BOTTOM);
        f.addComponent(combined);
        return f;
    }

    private Form getMoreButtonsForm() throws IOException {
        Form f = new Form("More Buttons");
        f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        addFormCommands(f);

        RadioButton rb;
        ButtonGroup group = new ButtonGroup();

        rb = new RadioButton("Grilled chicken");
        group.add(rb);
        f.addComponent(rb);

        rb = new RadioButton("Filet mignon");
        group.add(rb);
        f.addComponent(rb);

        rb = new RadioButton("Mahi mahi");
        group.add(rb);
        f.addComponent(rb);

        rb = new RadioButton("Chili");
        group.add(rb);
        f.addComponent(rb);

        CheckBox cb;

        cb = new CheckBox("Guacamole");
        f.addComponent(cb);

        cb = new CheckBox("Tabasco sauce");
        f.addComponent(cb);

        cb = new CheckBox("Mango salsa");
        f.addComponent(cb);

        cb = new CheckBox("Mayonnaise");
        f.addComponent(cb);

        cb = new CheckBox("Whipped cream");
        f.addComponent(cb);

        List list = new List();
        list.addItem("uno");
        list.addItem("due");
        list.addItem("tre");
        list.addItem("quattro");
        list.addItem("cinque");
        list.addItem("sei");
        f.addComponent(list);

        ComboBox comboBox = new ComboBox(list.getModel());
        f.addComponent(comboBox);
        return f;
    }

    private Form getListModelForm() throws IOException {
        Form f = new Form("More Buttons");
        f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));

        addFormCommands(f);
        List list = new List();
        list.addItem("uno");
        list.addItem("due");
        list.addItem("tre");
        list.addItem("quattro");
        list.addItem("cinque");
        list.addItem("sei");
        f.addComponent(list);

        ComboBox comboBox = new ComboBox(list.getModel());
        f.addComponent(comboBox);

        return f;
    }

    private Form getBorderLayoutForm() throws IOException {
        Form f = new Form("BorderLayout");
        f.setLayout(new BorderLayout());

        addFormCommands(f);

        Image image = Image.createImage("/res/P1020776r.JPG");
        Label bottomText = new Label(image);
        bottomText.setAlignment(Component.CENTER);
        bottomText.setText("Baldassare");
        bottomText.setTextPosition(Component.BOTTOM);

        Container buttonBar = new Container(new BoxLayout(BoxLayout.X_AXIS));
        Button addButton = new Button("Add");

        addButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        buttonBar.addComponent(addButton);
        buttonBar.addComponent(new Button("Remove"));
        buttonBar.addComponent(new Button("Edit"));
//        buttonBar.addComponent(new Button("Send"));
//        buttonBar.addComponent(new Button("Exit"));
        

        f.addComponent(BorderLayout.CENTER, bottomText);
        f.addComponent(BorderLayout.SOUTH, buttonBar);
        return f;
    }

//    private Form getTableForm() {
//        Form mainForm = new Form("Table Layout");
//        TableLayout layout = new TableLayout(4, 3);
//        mainForm.setLayout(layout);
//        TableLayout.Constraint constraint = layout.createConstraint();
//        constraint.setVerticalSpan(2);
//        constraint.setWidthPercentage(50);
//        mainForm.addComponent(constraint, new Label("First"));
//        mainForm.addComponent(new Label("Second"));
//        mainForm.addComponent(new Label("Third"));
//
//        constraint = layout.createConstraint();
//        constraint.setHeightPercentage(20);
//        mainForm.addComponent(constraint, new Label("Forth"));
//        mainForm.addComponent(new Label("Fifth"));
//        constraint = layout.createConstraint();
//        constraint.setHorizontalSpan(3);
//        Label span = new Label("Spanning");
//        span.getStyle().setBorder(Border.createLineBorder(2));
//        span.setAlignment(Component.CENTER);
//        mainForm.addComponent(constraint, span);
//        mainForm.show();
//        return mainForm;
//    }

    private void addFormCommands(Form form) {
        Command exitCommand = new Command("Exit");
        form.addCommand(exitCommand);
        form.addCommandListener(this);
    }

    public void startApp() {

        try {

            displayInit();

            Form f = null;

//            f = getImagesForm();
//            f = getImagesButtonsForm();
//            f = getMoreButtonsForm();
//            f = getListModelForm();
            f = getBorderLayoutForm();

            f.show();

        } catch (IOException ex) {
            // Do something here.
            ex.printStackTrace();
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void actionPerformed(ActionEvent ae) {
        notifyDestroyed();
    }
}
